﻿Imports System.Text
Imports System.Threading
Imports System.Web
Imports System.Runtime.InteropServices
Imports Sy.UI
Imports Sy.UI.Cef
Imports Sy.Data
Imports Sy.UI.Common
Imports System.ComponentModel

Public Class Form6
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            OpenFileDialog1.Filter = "csv files(*.csv)|*.csv"
            OpenFileDialog1.InitialDirectory = Application.StartupPath
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                imgpath.Text = OpenFileDialog1.FileName
                Dim alltext = My.Computer.FileSystem.ReadAllText(imgpath.Text, Encoding.Default)
                Dim stext = Split(alltext, vbLf)
                For i = 1 To UBound(stext)
                    If stext(i) <> "" Then
                        Dim nstr = Split(stext(i), ",")

                        Dim vtime = Replace(nstr(1), """", "")
                        Dim from = Replace(nstr(3), """", "")
                        Dim kwds = Replace(nstr(4), """", "")
                        Dim swds = Replace(nstr(5), """", "")
                        Dim frompage = Replace(nstr(7), """", "")
                        Dim ip = Replace(nstr(8), """", "")
                        Dim isold = Replace(nstr(13), """", "")
                        If vtime <> "" Then
                            Dim t As New TaskList3
                            t.vtime = vtime
                            t.from = from
                            t.kwds = kwds
                            t.swds = swds
                            t.frompage = frompage
                            t.ip = ip
                            t.isold = isold
                            t.Add()
                        End If
                    End If
                    'Console.WriteLine(stext(i))
                Next
                Loadlist("")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Sub Loadlist(sqlstr As String)
        Try
            With ListView1
                .Items.Clear()
            End With
            Dim TaskLists = If(sqlstr.IsNotWhiteSpace, TaskList3.LoadWhere(sqlstr), TaskList3.Load())
            Dim i As Long = 1
            For Each TaskList In TaskLists
                Dim a = UIAdapter.GetListViewItem(TaskList, i)
                Dim itm As ListViewItem = ListView1.Items.Add(a) '状态 进度 id
                itm.Tag = TaskList
                i = i + 1
            Next
        Catch ex As Exception
            MsgBox("加载数据异常", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            Dim url = "https://www.miwaimao.com/softlogin.php?UserName=" & UserNamelo & "&Version=" & Version & "&step=2&logintime=" & logintime
            Console.WriteLine(url)
            Dim html = getnewhtml(url)
            Console.WriteLine(html)
            If html = "8" Then
                MsgBox("您的账户已在别处登录，当前会话退出！")
                Timer1.Enabled = False
                End
            End If
        Catch ex As Exception
            MsgBox("登录异常，当前会话退出！")
            Timer1.Enabled = False
            End
        End Try
    End Sub

    Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Create = CreateSQLBuilder(Of TaskList3).CreateSQL()
        DB = New SQLite(Create, Application.StartupPath & "\Config\task3.db")
        TaskList3.AddDB(DB)
        DB.Foreign_keys = True
        UIAdapter.ListviewAdapter(Of TaskList3)(ListView1, "序号")
        Loadlist("")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            shuchu.Text = ""
            Dim allstr = ""
            Dim sql As String = "select * from t_TaskList3 where frompage like '%test.php%';"
            Dim reader As Data.Common.DbDataReader = DB(sql)
            Do While reader.Read
                Dim ip = reader("ip").ToString
                Dim sqls As String = "select * from t_TaskList3 where ip = '" & ip & "';"
                Dim readers As Data.Common.DbDataReader = DB(sqls)
                Do While readers.Read
                    If readers("kwds").ToString <> "--" Or readers("swds").ToString <> "--" Then
                        Dim keywds = "关键词：" & readers("kwds").ToString & ",搜索词：" & readers("swds").ToString & ",ip:" & readers("ip").ToString & ",来源：" & readers("from").ToString & vbCrLf
                        If InStr(allstr, keywds) < 1 Then
                            allstr &= keywds
                            shuchu.Text &= keywds
                        End If
                    End If
                Loop
                readers.Close()
            Loop
            reader.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


    Private Sub Form6_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        End
    End Sub
End Class